package cn.iocoder.yudao.module.system.controller.app.merchant;


import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.system.controller.app.merchant.vo.AppFellowPageReqVO;
import cn.iocoder.yudao.module.system.controller.app.merchant.vo.AppFellowReqVO;
import cn.iocoder.yudao.module.system.controller.app.merchant.vo.AppFellowRespVO;
import cn.iocoder.yudao.module.system.controller.app.merchant.vo.AppSetFellowReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.merchant.FellowMerchantDO;
import cn.iocoder.yudao.module.system.dal.dataobject.merchant.MerchantDO;
import cn.iocoder.yudao.module.system.service.merchant.MerchantService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 关注商家")
@RestController
@RequestMapping("/merchant/follow")
public class AppFollowMerchantController {
    @Resource
    private MerchantService merchantService;
    @Resource
    private AdminUserService userService;

    @PostMapping(value = "/follow")
    @Operation(summary = "关注商家")
    @PreAuthenticated
    public CommonResult<String> followMerchant(@RequestBody @Valid AppSetFellowReqVO vo) {
        merchantService.followMerchant(vo.getMerchantId());
        return CommonResult.success("关注成功");
    }


    @PostMapping("/unFollow")
    @Operation(summary = "取消关注")
    @PreAuthenticated
    public CommonResult<String> unFellow(@RequestBody @Valid AppSetFellowReqVO vo) {
        merchantService.unFellow(vo.getMerchantId());
        return CommonResult.success("已取消关注");
    }

    @GetMapping(value = "/page")
    @Operation(summary = "获得关注分页")
    @PreAuthenticated
    public CommonResult<PageResult<AppFellowRespVO>> getFellowPage(AppFellowPageReqVO reqVO) {
        AppFellowReqVO pageReqVO = BeanUtils.toBean(reqVO, AppFellowReqVO.class)
                .setUserId(getLoginUserId())
                .setUserDeleted(false);
        PageResult<FellowMerchantDO> pageResult = merchantService.getFellowPage(pageReqVO);
        Set<Long> merchantIds = convertSet(pageResult.getList(), FellowMerchantDO::getMerchantId);
        Map<Long, MerchantDO> merchantMap = convertMap(merchantService.getMerchantList(merchantIds), MerchantDO::getMerchantId);

        return success(BeanUtils.toBean(pageResult, AppFellowRespVO.class,
                vo -> Optional.ofNullable(merchantMap.get(vo.getMerchantId()))
                        .ifPresent(merchant -> vo.setMerchantName(merchant.getMerchantName())
                                .setLogo(merchant.getLogo()))));
    }

}
